<template>
  <view class="station-item-wrap" :class="styleBox">
    <!-- 电站名称 -->
    <view class="name-wrap">
      <text v-if="data.deletePrice" class="activity">惠</text>
      <text class="name text-overflow text-bold">{{ item.stationName }}</text>
    </view>
    <!-- 桩数量 -->
    <view class="pile">
      <view class="pile-item">
        <view class="test1 test1-direct">直</view>
        <text class="test2 test2-direct">闲{{ item.countFreeConnectorDC }}</text>
        <text>/共{{ item.countConnectorDC }}</text>
      </view>
      <view class="pile-item item-interflow">
        <view class="test1 test1-interflow">交</view>
        <text class="test2 test2-interflow">闲{{ item.countFreeConnectorAC }}</text>
        <text>/共{{ item.countConnectorAC }}</text>
      </view>
    </view>
    <!-- 电站地址 -->
    <view class="msg">
      <view class="iconfont icon-map"></view>
      <text class="msg-test text-overflow">{{ item.areaCodeName }}{{ item.address }}</text>
    </view>
    <!-- 停车说明 -->
    <view class="msg stop">
      <view class="iconfont icon-stop"></view>
      <text class="msg-test text-overflow">{{ item.parkExplain }}</text>
    </view>
    <!-- 电站标签 -->
    <!-- <view v-if="item.labelList && item.labelList.length > 0" class="labels">
      <view v-for="(temp, index) in item.labelList" :key="index">
        <view v-if="index < 3" class="label-item">{{ temp.labelName }}</view>
      </view>
      <view v-if="item.labelList.length > 3" class="label-item1">...</view>
    </view> -->
    <view v-if="data.labelStr && data.labelStr.length > 0" class="labels">
      <view v-for="(temp, index) in data.labelStr" :key="index">
        <view class="label-item">{{ temp }}</view>
      </view>
    </view>
    <!-- 收藏 -->
    <view class="iconfont collect" :class="[item.collectStatus ? 'icon-collect1' : 'icon-collect']"
      @tap.stop="tapCollect(item)"></view>
    <!-- 定位 -->
    <view class="distance" @tap.stop="tapDistance(item)">
      <view class="iconfont icon-exclude"></view>
      <text>{{ item.distance ? `${(item.distance / 1000).toFixed(1)}km` : '--' }}</text>
    </view>
    <!-- 原价格 -->
    <view v-if="data.deletePrice" class="elec-price">{{ data.deletePrice }}</view>
    <!-- 价格 -->
    <view class="discount-price">
      {{ data.initialPrice || '--' }}
      <text v-if="data.initialPrice" class="unit">/度</text>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { openLocation } from '@/utils/location';
import { stationCollect } from './station';
import { isNull } from 'lodash';

const emit = defineEmits(['locationOpenBtm']);

interface Props {
  item: any;
  styleBox: string;
  page?: string;
}
const prop = withDefaults(defineProps<Props>(), {
  item: {} as any,
  styleBox: '',
  page: '',
});

const data = reactive({
  initialPrice: '', // 原价
  deletePrice: '', // 划线价格
  labelStr: [] as Array<string>, // 显示标签
});

const getLabel = () => {
  data.labelStr = [];
  if (prop.item.labelList && prop.item.labelList.length > 0) {
    prop.item.labelList.forEach((item: any) => {
      data.labelStr.push(item.labelName);
    });
  }
  if (prop.item.serviceSetup) {
    const arr = prop.item.serviceSetup.split(',');
    data.labelStr = [...data.labelStr, ...arr];
  }
};
getLabel();

const getPrice = () => {
  if (!prop.item.hasOwnProperty('initialPrice')) return;
  const initialPrice = prop.item.initialPrice;
  const activityPrice = prop.item.activityPrice;
  if (activityPrice) {
    // 有优惠，展示优惠价格
    data.initialPrice = `￥${activityPrice}`;
    if (initialPrice > Number(activityPrice)) {
      // 原价>优惠价 展示划线价格-原价
      data.deletePrice = `￥${initialPrice.toFixed(4)}/度`;
    } else {
      // 否则 不展示划线价格
      data.deletePrice = '';
    }
  } else {
    // 否则只展示原价
    data.initialPrice = isNull(initialPrice) ? '--' : `￥${initialPrice.toFixed(4)}`;
    data.deletePrice = '';
  }
};
getPrice();
watch(
  () => prop.item,
  () => {
    getPrice();
    getLabel();
  }
);

// 收藏
const tapCollect = (item: any) => {
  stationCollect(item, prop.page);
};
// 定位
const tapDistance = (item: any) => {
  // #ifdef H5
  emit("locationOpenBtm", item)
  // #endif
  // #ifndef H5
  openLocation(item);
  // #endif
};
</script>

<style lang="scss" scoped>
.station-item-wrap {
  position: relative;
  padding: 16px;
  background-color: #fff;
  font-size: 12px;
  border-radius: 12px;

  &.list-item {
    margin: 8px 12px;
  }

  &.map-item {}

  .name-wrap {
    display: flex;
    align-items: center;
    padding-bottom: 8px;
    width: 220px;
    height: 27px;
    line-height: 27px;
    font-size: 18px;
    color: #000;

    .name {
      width: 220px;
    }

    .activity {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 18px;
      height: 18px;
      margin-right: 4px;
      font-size: 12px;
      color: #fff;
      background-color: #ffc700;
      border-radius: 4px;
    }
  }

  .labels {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    max-height: 44px;
    margin-top: 8px;
    overflow: hidden;

    .label-item {
      line-height: 18px;
      padding: 0 4px;
      margin-right: 8px;
      margin-bottom: 4px;
      color: $uni-color-primary;
      border: 1px solid $uni-color-primary;
      border-radius: 4px;
    }

    .label-item1 {
      line-height: 18px;
      color: $uni-color-primary;
    }
  }

  .msg {
    display: flex;
    align-items: center;
    width: 240px;
    line-height: 18px;
    color: #545454;
    margin-bottom: 6px;

    .iconfont {
      display: inline;
      margin-right: 6px;
      font-size: 14px;
    }

    .icon-map {
      font-size: 16px;
      margin-right: 4px;
    }

    .msg-test {
      width: calc(100% - 60px);
      line-height: 14px;
    }

    &.stop {
      margin-bottom: 0;
    }
  }

  .pile {
    display: flex;
    margin-bottom: 8px;
    color: #b5b5b5;

    .pile-item {
      display: flex;
      align-items: center;
      margin-right: 8px;
      padding: 2px;
      min-width: 65px;
      height: 17px;
      border: 1px solid #01c68b;
      border-radius: 4px;

      &.item-interflow {
        border: 1px solid #9859ff;
      }

      .test1 {
        width: 18px;
        height: 17px;
        line-height: 17px;
        text-align: center;
        color: #fff;
        border-radius: 2px;
      }

      .test1-direct {
        background-color: #01c68b;
      }

      .test1-interflow {
        background-color: #9859ff;
      }

      .test2 {
        margin-left: 4px;
      }

      .test2-direct {
        color: #01c68b;
      }

      .test2-interflow {
        color: #9859ff;
      }
    }
  }

  .distance {
    position: absolute;
    top: 6px;
    right: 6px;
    padding: 10px;
    text-align: center;

    .icon-exclude {
      margin-bottom: 8px;
      font-size: 22px;
      color: $uni-color-primary;
    }

    .text {
      line-height: 18px;
      font-size: 12px;
      color: #000;
    }
  }

  .collect {
    position: absolute;
    top: 7.5px;
    right: 60px;
    padding: 10px;
    font-size: 19px;
    color: $uni-color-primary;
  }

  .elec-price {
    position: absolute;
    top: 68px;
    right: 16px;
    line-height: 22px;
    font-size: 14px;
    color: #b5b5b5;
    text-decoration: line-through;
  }

  .discount-price {
    position: absolute;
    top: 88px;
    right: 16px;
    line-height: 34px;
    font-size: 24px;

    .unit {
      font-size: 14px;
    }
  }
}
</style>